// router/index.js

import modules from './modules'
import Vue from 'vue'
//这里仅示范npm安装方式的引入，其它方式引入请看最上面【安装】部分
import Router from 'uni-simple-router'


Vue.use(Router)
const whitelist = { //声明了一个白名单，为tabBar下的页面
	'/pages/wxlogin/jump': 'jump',
};
//初始化
const router = new Router({
	routes: [...modules] //路由表
});
//全局路由前置守卫 
router.beforeEach((to, from, next) => {
	if (whitelist[to.path]) {
		next(to.url);
	} else {
		if (uni.getStorageSync('h5_wx_code')) {
			uni.request({
				url: 'http://www.mksoft.cn/zx/front/user/getUserInfo',
				header: {
					token: uni.getStorageSync('h5_wx_code')
				},
				success: (res) => {
					console.log(uni.getStorageSync('h5_wx_code'));
					console.log(res);
					if (res.data.code ==  200) {
						next();
					} else {
						// 未登录
						if (to.path == '/pages/wxlogin/wxlogin') {
							next();
						} else {
							next({
								path: '/pages/wxlogin/wxlogin',
								params: {
									routePath: encodeURIComponent(to.path),
								},
								NAVTYPE: 'push'
							});
						}
					}
				}
			})
		} else {
			// 未登录
			if (to.path == '/pages/wxlogin/wxlogin') {
				next();
			} else {
				next({
					path: '/pages/wxlogin/wxlogin',
					params: {
						routePath: encodeURIComponent(to.path),
					},
					NAVTYPE: 'push'
				});
			}
 
		}
	}
})
   
// 全局路由后置守卫
router.afterEach((to, from) => {})
export default router;
